package com.example.sms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.sms.entity.Course;
import com.example.sms.entity.Student;
import com.example.sms.entity.Teacher;
import com.example.sms.entity.dto.LoginDTO;
import com.example.sms.entity.dto.RegisterDTO;
import com.example.sms.exception.SimpleException;
import com.example.sms.mapper.CourseMapper;
import com.example.sms.mapper.StudentMapper;
import com.example.sms.mapper.TeacherMapper;
import com.example.sms.response.ResponseModel;
import com.example.sms.service.CourseService;
import com.example.sms.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper , Teacher> implements TeacherService {
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private CourseService courseService;
    @Autowired
    private CourseMapper courseMapper;

    @Override
    public Teacher login(LoginDTO loginDTO) {
        if (StringUtils.isBlank(loginDTO.getUsername())){
            throw new SimpleException("用户名不能为空");
        } else if (StringUtils.isBlank(loginDTO.getPassword())) {
            throw new SimpleException("密码不能为空");
        }
        if (teacherMapper.checkByUsernameAndPassword(loginDTO.getUsername() , loginDTO.getPassword())){
            return teacherMapper.selectOne(new QueryWrapper<Teacher>().lambda()
                    .eq(Teacher::getUsername, loginDTO.getUsername())
                    .eq(Teacher::getPassword, loginDTO.getPassword()));
        }else {
            throw new SimpleException("用户名或密码错误");
        }
    }

    @Override
    public Integer register(RegisterDTO registerDTO) {
        return teacherMapper.insert(registerDTO);
    }

    @Override
    public List<String> getCourses(Integer teacherId) {
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(Course::getTeacherId, teacherId);

        // 使用 courseMapper 执行查询
        List<Course> courseList = courseMapper.selectList(queryWrapper);

        // 提取 courseName
        List<String> courseNames = new ArrayList<>();
        for (Course course : courseList) {
            courseNames.add(course.getCourseName());
        }
        return courseNames;
    }


    @Override
    public Integer addCourse(Integer teacherId, Integer courseId) {
        return courseService.addToTeacherCourse(courseId,teacherId);
    }

    @Override
    public List<Course> getTeacherCourses(Integer teacherId) {
//        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
//        queryWrapper.lambda()
//                .eq(Course::getTeacherId, teacherId);
//        return courseService.list(queryWrapper);
        return courseService.getCoursesByTeacherId(teacherId);
    }


    @Override
    public Integer delStudent(Integer teacherId, Integer studentId) {
        return teacherMapper.delStudent(teacherId, studentId);
    }


}
